package com.luo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.luo.constant.MessageConstant;
import com.luo.constant.RedisMessageConstant;
import com.luo.entity.Result;
import com.luo.service.OrderService;
import com.luo.util.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @author Zoro
 * @date 2021年08月30
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping("/submit.do")
    public Result submit(@RequestBody Map map) {
        //在页面获取手机号
        String telephone = (String) map.get("telephone");
        //在页面获取验证码
        String validateCode = (String) map.get("validateCode");
        //从redis中根据key获取相应的验证码
        String redisValidateCode = jedisPool.getResource().get(RedisMessageConstant.SENDTYPE_ORDER + telephone);
        if (!validateCode.equals(redisValidateCode)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        Result result = null;
        try {
            result = orderService.submit(map);
            SMSUtils.sendShortMessage(telephone, "您以预约成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/findById.do")
    public Map findById(Integer id) {
        return orderService.findById(id);
    }

}
